/*
 * BLLib.Configuration.(UITypeEditors)
 * Andy Tidball
 * 
 * Project: Black Lightning General Library
 * Copyright: GNU General Public License
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace BLLib.Configuration {
	/// <summary>
	/// A UITypeEditor for entering passwords and other text which cannot be displayed.
	/// </summary>
	public class PasswordTypeEditor : UITypeEditor {
		// member data
		private PasswordEditForm _EditForm; // the form used to edit the password value

		/// <summary>
		/// Creates a new PasswordTypeEditor.
		/// </summary>
		public PasswordTypeEditor() {
			_EditForm = new PasswordEditForm();
		}

		/// <summary>
		/// Gets the EditStyle used by this UITypeEditor.
		/// </summary>
		/// <param name="Context">Contextual information about what is being edited.</param>
		/// <returns>The EditStyle to use.</returns>
		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext Context) {
			return UITypeEditorEditStyle.Modal;
		}

		/// <summary>
		/// Edits the given value using this UITypeEditor.
		/// </summary>
		/// <param name="Context">Contextual information about what is being edited.</param>
		/// <param name="Provider">Provides services?</param>
		/// <param name="Value">The value to edit.</param>
		/// <returns>The new value after having been edited.</returns>
		public override object EditValue(ITypeDescriptorContext Context, IServiceProvider Provider, object Value) {
			// set the current value and show the dialog
			_EditForm.Password = Value as string;
			IWindowsFormsEditorService Service = Provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
			if (Service.ShowDialog(_EditForm) != DialogResult.OK) {
				return Value;
			}

			// return the new value
			return _EditForm.Password;
		}
	}

	/// <summary>
	/// A form used to edit a password value.
	/// </summary>
	public class PasswordEditForm : Form {
		// member data - controls
		private TextBox txtPassword; // the textbox used to enter the password
		private Button  btnOK;       // the OK button
		private Button  btnCancel;   // the Cancel button

		/// <summary>
		/// Creates a new PasswordEditForm.
		/// </summary>
		public PasswordEditForm() {
			// initialize our controls
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the password being edited in the form.
		/// </summary>
		public string Password {
			get {
				return txtPassword.Text;
			}
			set {
				txtPassword.Text = value;
			}
		}

		/// <summary>
		/// Initializes all of our controls.
		/// </summary>
		private void InitializeComponent() {
			// create our controls
			txtPassword = new TextBox();
			btnOK       = new Button();
			btnCancel   = new Button();

			// txtPassword
			txtPassword.Dock         = DockStyle.Top;
			txtPassword.Font         = new Font(Control.DefaultFont.FontFamily, 12, FontStyle.Bold);
			txtPassword.Name         = "txtPassword";
			txtPassword.PasswordChar = '*';
			txtPassword.TabIndex     = 1;

			// btnOK
			btnOK.Name     = "btnOK";
			btnOK.Location = new Point(21, 34);
			btnOK.TabIndex = 2;
			btnOK.Text     = "OK";
			btnOK.Click   += btnOK_Click;
			// btnCancel
			btnCancel.Name     = "btnCancel";
			btnCancel.Location = new Point(104, 34);
			btnCancel.TabIndex = 3;
			btnCancel.Text     = "Cancel";
			btnCancel.Click   += btnCancel_Click;

			// this
			this.AcceptButton    = btnOK;
			this.CancelButton    = btnCancel;
			this.ClientSize      = new Size(200, 65);
			this.DialogResult    = DialogResult.Cancel;
			this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
			this.Name            = "PasswordEditForm";
			this.ShowInTaskbar   = false;
			this.StartPosition   = FormStartPosition.CenterParent;
			this.Text            = "Change Password";
			this.Controls.Add(txtPassword);
			this.Controls.Add(btnOK);
			this.Controls.Add(btnCancel);
			this.Shown += this_Shown;
		}

		/// <summary>
		/// Called when the form is shown.
		/// </summary>
		/// <param name="Sender">The Form being shown.</param>
		/// <param name="e">No arguments.</param>
		private void this_Shown(object Sender, EventArgs e) {
			txtPassword.SelectAll();
		}

		/// <summary>
		/// Called when the OK button is clicked.
		/// </summary>
		/// <param name="Sender">The Button that was clicked.</param>
		/// <param name="e">No arguments.</param>
		private void btnOK_Click(object Sender, EventArgs e) {
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		/// <summary>
		/// Called when the Cancel button is clicked.
		/// </summary>
		/// <param name="Sender">The Button that was clicked.</param>
		/// <param name="e">No arguments.</param>
		private void btnCancel_Click(object Sender, EventArgs e) {
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
	}
}